#pragma once
#include "StdAfx.h"

/*
	Binding class. Has a Name-Aux string pair that allows to store binding records and search by name through BBVector.
	Used to parse config files of form [+|-|;](.*)=(.*) that we use to pass information to the program through textual files.

	Arseni Natapov, 1/14/2011
*/
class Binding
{
public:
	Binding(string name, string aux)
	{
		Name=name;
		Aux=aux;
	};
	Binding()
	{
		Name="";
		Aux="";
	}
	string Aux, Name;
};
/*
	Reader class. Is used to open and parse files through regular expressions.

	Arseni Natapov, 1/14/2011
*/
class GameResource;
class Reader
{
public:
	//vector of std::strings used to return parsings
	typedef std::vector<std::string> strVector;
	//binding parsing specific typedefs
	typedef std::pair<std::string, std::string> strPair;
	typedef std::pair<std::string::const_iterator, std::string::const_iterator> itPair;
	typedef std::vector<strPair> strPairVector;
	

	Reader(void);
	Reader(string);
	~Reader(void);
	
	void LoadFile(string);
	void SaveFile(string);
	//get bindings from the file that has been read into File
	BBVector<sptr<Binding>> GetBindings(string identifier);
	//the parsing function that parses string str into a string vector with the expression given
	//the user must keep track of how many substrings per match the vector must contain 
	strVector Parse(std::string &str, std::string expression);
	strVector TokenizeSTD(std::string &str);
	//the contents of the file
	std::string File;
	bool JumpAfterNext(std::string);
	Vec3 GetVec3();
	int GetInt();
	real GetReal();
	string GetQuotedString();
	string GetToken();
	string GetNext(string);
	sptr<GameResource> GetResc(string);
	std::string::const_iterator GetItBefore(std::string);
	std::string::const_iterator GetItAfter(std::string);
	void Reset();
	bool eof();
private:
	enum eRegex
	{
		RegexBinding,
		RegexPair,
		RegexBlock,
		RegexReal,
		RegexInt,
		RegexQuotedString,
		RegexToken
	};
	//the path to the file
	string FileName;
	std::string::const_iterator CurPos;
	std::string lastRegex;
	boost::regex Regex;
	boost::match_results<std::string::const_iterator> Matches; 
    boost::match_flag_type Flags;
	//read file helper function
	std::string ReadFile(string);
	//helper functions for config parsing
	//parses the string into pairs
	strPairVector ParseIntoPairs(std::string);
	itPair GetNextOccurence(std::string);
	//parses the pairs into bindings
	sptr<Binding> ParseIntoBinding(std::string);
	void SetRegex(std::string);
	string GetNext();
	void SetGetRealRegex();
};

